<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>计算器</title>
    <link href="../_assets/css/shared.css" rel="stylesheet" type="text/css"/>
    <link href="../_assets/css/examples.css" rel="stylesheet" type="text/css"/>
    <script src="../_assets/js/examples.js"></script>
    <script src="../lib/easeljs-NEXT.js"></script>   
    <!-- We also provide hosted minified versions of all CreateJS libraries.
      http://code.createjs.com -->
    <script src="../_assets/libs/preloadjs-NEXT.min.js"></script>

    <script id="editable">
        var update = true;
        var calculationContainer = new createjs.Container();
        var numText = "";
        var mrNum = "";
        var opNum1 = "";
        var opNum2 = "";
        var percentNum = "";
        var op = "";
        var text = rendText(numText);
        var lastResult = "";
        var result = "";
              
        var flagPoint = true;
        var flagEqual = true;
        var flagPercent = true;
        var flagMr = true;
        var flagOp = true
        var flagRe = true;
        var flagMax = true;

        function init() {

            stage = new createjs.Stage("calculation");

            w = stage.canvas.width;
            h = stage.canvas.height;
            stage.enableMouseOver(10);
            manifest = [
                    {src: "panel.png", id: "panel"},//计算器面板
                    {src: "ac.png", id: "ac"},//清零按钮
                    {src: "0.png", id: "zero"},//数字0
                    {src: "00.png", id: "doubleZero"},//数字00
                    {src: "1.png", id: "one"},//数字1
                    {src: "2.png", id: "two"},//数字2
                    {src: "3.png", id: "three"},//数字3
                    {src: "4.png", id: "four"},//数字4
                    {src: "5.png", id: "five"},//数字5
                    {src: "6.png", id: "six"},//数字6
                    {src: "7.png", id: "seven"},//数字7
                    {src: "8.png", id: "eight"},//数字8
                    {src: "9.png", id: "nine"},//数字1
                    {src: "mc.png", id: "mc"},//mc按钮
                    {src: "mr.png", id: "mr"},//mr按钮
                    {src: "m-.png", id: "m-"},//m-按钮
                    {src: "m+.png", id: "m+"},//m+按钮
                    {src: "_.png", id: "delete"},//mc按钮
                    {src: "..png", id: "point"},//点.
                    {src: "+.png", id: "add"},//加号
                    {src: "=.png", id: "equal"},//等于号
                    {src: "-.png", id: "subt"},//减号
                    {src: "%.png", id: "percent"},//百分号  
                    {src: "x.png", id: "mul"},//乘号
                    {src: "division.png", id: "div"},//除号        
                    {src: "sqrt.png", id: "sqrt"},//根号 
                    {src: "+-.png", id: "plusOrMinus"},//正负号
            ];


            loader = new createjs.LoadQueue(false);
            loader.addEventListener("complete", handleComplete);
            loader.loadManifest(manifest, true, "calculation_images/");
        }

        function handleComplete() {
            panelImg = loader.getResult("panel");
            acImg = loader.getResult("ac");
            zeroImg = loader.getResult("zero");
            doubleZeroImg = loader.getResult("doubleZero");
            
            oneImg = loader.getResult("one");
            twoImg = loader.getResult("two");
            threeImg = loader.getResult("three");
            fourImg = loader.getResult("four");
            fiveImg = loader.getResult("five");
            sixImg = loader.getResult("six");
            sevenImg = loader.getResult("seven");
            eightImg = loader.getResult("eight");
            nineImg = loader.getResult("nine");

            pointImg = loader.getResult("point");
            addImg = loader.getResult("add");
            subtImg = loader.getResult("subt");
            divImg = loader.getResult("div");
            mulImg = loader.getResult("mul");
            equalImg = loader.getResult("equal");
            perImg = loader.getResult("percent");
            pmImg = loader.getResult("plusOrMinus");
            sqrtImg = loader.getResult("sqrt");
            mcImg = loader.getResult("mc");
            mrImg = loader.getResult("mr");
            msubtImg = loader.getResult("m-");
            maddImg = loader.getResult("m+");
            deleteImg = loader.getResult("delete");

            

            var panel = rendPanel(panelImg,100,100,panelImg.width,panelImg.height);

            
            //清零按钮
            var acBut = rendButton(acImg,195+zeroImg.width*3 + addImg.width,215,acImg.width,acImg.height);
            acBut.addEventListener("mousedown",function(){
                var child = stage.children[0];
                var cLength = child.numChildren;

                if(!flagRe){
                    child.removeChildAt(cLength-1);
                    flagRe = true;
                }
                numText = "0";
                op ="";
                opNum1 = "";
                opNum2 = "";
                result = "";
                text.text = numText;
                flagPoint = true;
                flagPm = true;

                calculationContainer.updateCache();
                update = true;
            });

            //数字0按钮
            var zeroBut = rendButton(zeroImg,145,425,zeroImg.width,zeroImg.height);
            zeroBut.addEventListener("mousedown",function(){
                reRendtext_num("0");
            });

            var doubleZeroBut = rendButton(doubleZeroImg,155+zeroImg.width,425,doubleZeroImg.width,doubleZeroImg.height);
            doubleZeroBut.addEventListener("mousedown",function(){
                if(Number(numText) == 0 && !(isPoint(numText).bool) && flagEqual){
                    reRendtext_num("0");
                }
                else{
                    if(!flagEqual){
                        reRendtext_num("0");
                    }
                    else{
                        reRendtext_num("00");
                    }
                    
                }
                
            });


            //数字1
            var oneBut = rendButton(oneImg,145,415 - zeroImg.height,oneImg.width,oneImg.height);
            oneBut.addEventListener("mousedown",function(){
                reRendtext_num("1");
            });

            var twoBut = rendButton(twoImg,155+oneImg.width,415 - zeroImg.height,twoImg.width,twoImg.height);
            twoBut.addEventListener("mousedown",function(){
                reRendtext_num("2");
            });


            var threeBut = rendButton(threeImg,165+oneImg.width*2,415 - zeroImg.height,threeImg.width,threeImg.height);
            threeBut.addEventListener("mousedown",function(){
                reRendtext_num("3");
            });


            var fourBut = rendButton(fourImg,145,405 - zeroImg.height*2,fourImg.width,fourImg.height);
            fourBut.addEventListener("mousedown",function(){
                reRendtext_num("4");
            });

            var fiveBut = rendButton(fiveImg,155+fourImg.width,405 - zeroImg.height*2,fiveImg.width,fiveImg.height);
            fiveBut.addEventListener("mousedown",function(){
                reRendtext_num("5");
            });

            var sixBut = rendButton(sixImg,165+fourImg.width*2,405 - sixImg.height*2,sixImg.width,fiveImg.height);
            sixBut.addEventListener("mousedown",function(){
                reRendtext_num("6");
            });

            var sevenBut = rendButton(sevenImg,145,395 - zeroImg.height*3,sevenImg.width,sevenImg.height);
            sevenBut.addEventListener("mousedown",function(){
                reRendtext_num("7");
            });

            var eightBut = rendButton(eightImg,155+sevenImg.width,395 - eightImg.height*3,eightImg.width,sevenImg.height);
            eightBut.addEventListener("mousedown",function(){
                reRendtext_num("8");
            });

            var ninetBut = rendButton(nineImg,165+sevenImg.width*2,395 - nineImg.height*3,nineImg.width,sevenImg.height);
            ninetBut.addEventListener("mousedown",function(){
                reRendtext_num("9");
            });

            var pointBut = rendButton(pointImg,165+zeroImg.width*2,425,pointImg.width,pointImg.height);
            pointBut.addEventListener("mousedown",function(){
                if(flagPoint && Number(numText) == 0 && flagEqual && flagMr){
                    numText = "0."
                    text.text = numText;
                    calculationContainer.updateCache();
                    update = true;
                    flagPoint = false;
                }
                else if(flagPoint && numText != "0" && flagEqual && flagMr){
                    numText = numText+".";
                    text.text = numText;
                    flagPoint = false;
                    calculationContainer.updateCache();
                    update = true;
                }
                else if(!flagEqual && flagMr){
                    numText = "0."
                    text.text = numText;
                    calculationContainer.updateCache();
                    update = true;
                    flagPoint = false;
                    flagEqual = true;
                }
                else if(!flagEqual && !flagMr){
                    numText = "0."
                    text.text = numText;
                    calculationContainer.updateCache();
                    update = true;
                    flagPoint = false;
                    flagMr = true;
                    flagEqual = true;
                }
                else if(flagEqual && !flagMr){
                    numText = "0."
                    text.text = numText;
                    calculationContainer.updateCache();
                    update = true;
                    flagPoint = false;
                    flagMr = true;
                }
            });



            var addtBut = rendButton(addImg,185+zeroImg.width*3,390,addImg.width,addImg.height);
            addtBut.addEventListener("mousedown",function(){
                reRendtext_op1("+");
            });

            var subtBut = rendButton(subtImg,185+zeroImg.width*3,380-subtImg.height,subtImg.width,subtImg.height);
            subtBut.addEventListener("mousedown",function(){
                reRendtext_op1("-");

            });

            var mulBut = rendButton(mulImg,195+zeroImg.width*3 + addImg.width,380-subtImg.height,mulImg.width,mulImg.height);
            mulBut.addEventListener("mousedown",function(){
                reRendtext_op1("*");
            });

            var perBut = rendButton(perImg,185+zeroImg.width*3,370-subtImg.height*2,perImg.width,perImg.height);
            perBut.addEventListener("mousedown",function(){
                if(opNum1 !="" && op !="" &&flagEqual){
                    opNum1 = opNum1.toString();
                    opNum2 = opNum2.toString();
                    var op1Length = opNum1.length;
                    var op2Length = opNum2.length;
                    
                    var point1 = isPoint(opNum1);
                    var point2 = isPoint(opNum2);
                    var pLength1 = op1Length - point1.location - 1;
                    var pLength2 = op2Length - point2.location - 1;

                    if(pLength1 >= pLength2 && op != "*"){
                        var length = pLength1;
                    }
                    else if(pLength1 <= pLength2 && op != "*"){
                        var length = pLength2;
                    }
                    else if(pLength1 >= pLength2 && op == "*"){
                        var length = pLength1+pLength2;
                    }
                    else if(pLength1 <= pLength2 && op == "*"){
                        var length = pLength1+pLength2;
                    }
                    console.log(length)
                    if(length == 0){
                        length =2;
                    }
                    
                    switch(op){
                        case "+": if(flagPercent){
                                      opNum2 = numText;
                                      opNum2 = (opNum2/100 * opNum1).toFixed(length);
                                      flagPercent = false;
                                  }
                                  numText = Number(opNum1) + Number(opNum2);
                                  
                                  break;
                        case "-": if(flagPercent){
                                      opNum2 = numText;
                                      opNum2 = opNum2/100 * opNum1;
                                      flagPercent = false;
                                  }
                                  numText = Number(opNum1) - Number(opNum2);
                                  break; 
                        case "*": if(flagPercent){
                                      opNum2 = numText;
                                      opNum2 = opNum2/100;
                                      flagPercent = false;
                                  }
                                  numText = Number(opNum1) * Number(opNum2);
                                  
                                  break;
                        case "/": if(flagPercent){
                                      opNum2 = numText;
                                      opNum2 = opNum2/100;
                                      flagPercent = false;
                                  }
                                  numText = Number(opNum1) / Number(opNum2);
                                  
                                  break;
                    }

                    console.log(opNum1,opNum2,op,numText)

                    opNum1 = opNum1.toString();
                    opNum2 = opNum2.toString();
                    var op1Length = opNum1.length;
                    var op2Length = opNum2.length;
                    
                    var point1 = isPoint(opNum1);
                    var point2 = isPoint(opNum2);
                    var pLength1 = op1Length - point1.location - 1;
                    var pLength2 = op2Length - point2.location - 1;

                    if(pLength1 >= pLength2 && op != "*"){
                        var length = pLength1;
                    }
                    else if(pLength1 <= pLength2 && op != "*"){
                        var length = pLength2;
                    }
                    else if(pLength1 >= pLength2 && op == "*"){
                        var length = pLength1+pLength2;
                    }
                    else if(pLength1 <= pLength2 && op == "*"){
                        var length = pLength1+pLength2;
                    }


                    numText = Number(numText);

                    if(!isE(numText)){
                        if(numText<=99999999 || numText == "Infinity"){
                            if(op != "/"){
                                numText = numText.toFixed(length);
                            }
                            console.log(length)
                            numText = numText.toString();
                            if(numText.length>9 && (isPoint(numText).bool) ){
                                numText = numText.slice(0,10);
                                if(numText.slice(9,10)=="."){
                                    text.font="bold 50px Arial";
                                    numText = numText.slice(0,9);
                                }      
                                else{
                                    text.font="bold 48px Arial";
                                }                 
                            }
                            else{
                            
                                numText = numText.slice(0,9);
                                text.font = "bold 50px Arial";

                                
                            }
                            
                            if(op == "/" && opNum2 == 0){
                                numText = 0;
                                if(flagRe){
                                    var eText = reRendtext_e();
                                    calculationContainer.addChild(eText);
                                    flagRe = false;
                                }
                            }

                        }
                        else{
                            result = numText;

                            result = result.toString();
                            if(result.length>9 && (isPoint(result).bool) ){
                                result = result.slice(0,10);
                                if(result.slice(9,10)=="."){
                                    text.font="bold 50px Arial";
                                    result = result.slice(0,9);
                                }      
                                else{
                                    text.font="bold 48px Arial";
                                }                 
                            }
                            else{
                            
                                result = result.slice(0,9);
                                text.font = "bold 50px Arial";


                                
                            }
                            numText = lastResult;

                        }
                    }
                    else{
                        numText ="0";
                    }

                    //console.log(opNum1,opNum2,op,result)
                    lastResult = numText;
                    if(result != ""){
                        text.font="bold 48px Arial";
                        text.text = result;
                        if(flagRe){
                            var eText = reRendtext_e();
                            calculationContainer.addChild(eText);
                            flagRe = false;
                        }
                    }
                    else{
                        text.text = numText;
                    }

                    calculationContainer.updateCache();
                    update = true;

                    flagPoint = true;
                    flagEqual = false;
                    opNum1 = "";
                    opNum2 = "";
                }
            });

            var divBut = rendButton(divImg,195+zeroImg.width*3 + addImg.width,370-subtImg.height*2,divImg.width,divImg.height);
            divBut.addEventListener("mousedown",function(){
                reRendtext_op1("/");
            });

            var pmBut = rendButton(pmImg,185+zeroImg.width*3,395 - nineImg.height*3,pmImg.width,pmImg.height);
            pmBut.addEventListener("mousedown",function(){
                if(numText >0 && (numText.toString()).length < 9 && !(isPoint(numText)).bool && numText != ""){
                    numText = "-" + numText;
                    text.text = numText;
                }
                else if(numText >0 && (numText.toString()).length < 10 && (isPoint(numText)).bool && numText != ""){
                    numText = "-" + numText;
                    text.text = numText;
                }
                else if(numText < 0 && numText != ""){
                    numText = numText.toString();
                    numText = numText.slice(1,numText.length);
                    text.text = numText;
                }
                else if(numText == ""){
                    if(opNum1 >0 && (opNum1.toString()).length < 9 && !(isPoint(opNum1)).bool && opNum1 != ""){
                    opNum1 = "-" + opNum1;
                    text.text = opNum1;
                    }
                    else if(opNum1 >0 && (opNum1.toString()).length < 10 && (isPoint(opNum1)).bool && opNum1 != ""){
                        opNum1 = "-" + opNum1;
                        text.text = opNum1;
                    }
                    else if(opNum1 < 0 && opNum1 != ""){
                        opNum1 = opNum1.toString();
                        opNum1 = opNum1.slice(1,opNum1.length);
                        text.text = opNum1;
                    }
                }
                    
                
                calculationContainer.updateCache();
                update = true;
            });

            var sqrtBut = rendButton(sqrtImg,195+zeroImg.width*3 + addImg.width,395 - nineImg.height*3,sqrtImg.width,sqrtImg.height);
            sqrtBut.addEventListener("mousedown",function(){

                numText = Math.sqrt(numText);
                numText = numText.toString();

                if(numText.length>9 && (isPoint(numText).bool) ){
                    numText = numText.slice(0,10);
                    if(numText.slice(9,10)=="."){
                        text.font="bold 50px Arial";
                        numText = numText.slice(0,9);
                    }
                    else{
                        text.font="bold 48px Arial";
                    }
                }
                else{
                    numText = numText.slice(0,9)
                }
                
                text.text = numText;
                calculationContainer.updateCache();
                update = true;
                flagPoint = true;
                flagEqual = false;
            });


            var equalBut = rendButton(equalImg,195+zeroImg.width*3 + addImg.width,390,equalImg.width,equalImg.height);
            equalBut.addEventListener("mousedown",function(){

                if(flagEqual){
                    if(numText != ""){
                        opNum2 = numText;
                    }
                    else{
                        opNum2 = lastResult;
                    }
                }
                
                if(opNum1 !="" && opNum2 != ""){
                    switch(op){
                        case "+": numText = Number(opNum1) + Number(opNum2);
                                  break;
                        case "-": numText = Number(opNum1) - Number(opNum2);
                                  break; 
                        case "*": numText = Number(opNum1) * Number(opNum2);
                                  break;
                        case "/": numText = Number(opNum1) / Number(opNum2);
                                  break;
                    }

                    opNum1 = opNum1.toString();
                    opNum2 = opNum2.toString();
                    var op1Length = opNum1.length;
                    var op2Length = opNum2.length;
                    
                    var point1 = isPoint(opNum1);
                    var point2 = isPoint(opNum2);
                    var pLength1 = op1Length - point1.location - 1;
                    var pLength2 = op2Length - point2.location - 1;

                    if(pLength1 >= pLength2 && op != "*"){
                        var length = pLength1;
                    }
                    else if(pLength1 <= pLength2 && op != "*"){
                        var length = pLength2;
                    }
                    else if(pLength1 >= pLength2 && op == "*"){
                        var length = pLength1+pLength2;
                    }
                    else if(pLength1 <= pLength2 && op == "*"){
                        var length = pLength1+pLength2;
                    }


                    numText = Number(numText);

                    if(!isE(numText)){
                        if(numText<=99999999 || numText == "Infinity"){
                            if(op != "/"){
                                numText = numText.toFixed(length);
                            }

                            numText = numText.toString();
                            if(numText.length>9 && (isPoint(numText).bool) ){
                                numText = numText.slice(0,10);
                                if(numText.slice(9,10)=="."){
                                    text.font="bold 50px Arial";
                                    numText = numText.slice(0,9);
                                }      
                                else{
                                    text.font="bold 48px Arial";
                                }                 
                            }
                            else{
                            
                                numText = numText.slice(0,9);
                                text.font = "bold 50px Arial";
                                
                            }
                            
                            if(op == "/" && opNum2 == 0){
                                numText = 0;
                                if(flagRe){
                                    var eText = reRendtext_e();
                                    calculationContainer.addChild(eText);
                                    flagRe = false;
                                }
                            }

                        }
                        else{
                            result = numText;

                            result = result.toString();
                            if(result.length>9 && (isPoint(result).bool) ){
                                result = result.slice(0,10);
                                if(result.slice(9,10)=="."){
                                    text.font="bold 50px Arial";
                                    result = result.slice(0,9);
                                }      
                                else{
                                    text.font="bold 48px Arial";
                                }                 
                            }
                            else{
                            
                                result = result.slice(0,9);
                                text.font = "bold 50px Arial";
                                
                            }
                            numText = lastResult;

                        }
                    }
                    else{
                        numText ="0";
                    }

                    //console.log(opNum1,opNum2,op,result)
                    lastResult = numText;
                    if(result != ""){
                        text.font="bold 48px Arial";
                        text.text = result;
                        if(flagRe){
                            var eText = reRendtext_e();
                            calculationContainer.addChild(eText);
                            flagRe = false;
                        }
                    }
                    else{
                        text.text = numText;
                    }
                    
                    calculationContainer.updateCache();
                    update = true;
                    flagPoint = true;
                    flagEqual = false;
                    
                    opNum1 = numText;
                    //opNum2 = "";
                    result = "";
                }

            });

            var mcBut = rendButton(mcImg,145,398 - zeroImg.height*4,mcImg.width,mcImg.height);
            mcBut.addEventListener("mousedown",function(){
                mrNum = 0;
            });

            var mrBut = rendButton(mrImg,155+sevenImg.width,398 - zeroImg.height*4,mrImg.width,mrImg.height);
            mrBut.addEventListener("mousedown",function(){
                mrNum = mrNum.toString();
                if(mrNum.length>9 && (isPoint(mrNum).bool) ){
                    mrNum = mrNum.slice(0,10);
                    if(mrNum.slice(9,10)=="."){
                        text.font="bold 50px Arial";
                        mrNum = mrNum.slice(0,9);
                        text.text = mrNum;
                    }
                }
                else{
                    mrNum = mrNum.slice(0,9)
                }

                numText = mrNum;
                text.text = numText;
                calculationContainer.updateCache();
                update = true;
                flagMr = false;
                flagPoint = true;
            });

            var msubtBut = rendButton(msubtImg,165+sevenImg.width*2,398 - zeroImg.height*4,msubtImg.width,msubtImg.height);
            msubtBut.addEventListener("mousedown",function(){
                
                numText = numText.toString();
                mrNum = mrNum.toString();
                var mLength = mrNum.length;
                var nLength = numText.length;

                var point1 = isPoint(numText);
                var point2 = isPoint(mrNum);
                var pLength1 = nLength - point1.location - 1;
                var pLength2 = mLength - point2.location - 1;

                if(pLength1 > pLength2){
                    var length = pLength1;
                }
                else{
                    var length = pLength2;
                }

                mrNum = Number(mrNum);
                numText = Number(numText);
                mrNum -= numText;
                if(length > 0){
                    mrNum = mrNum.toFixed(length);
                }

                flagMr = false;
            });


            var maddBut = rendButton(maddImg,185+sevenImg.width*3,398 - zeroImg.height*4,maddImg.width,maddImg.height);
            maddBut.addEventListener("mousedown",function(){
                numText = numText.toString();
                mrNum = mrNum.toString();
                var mLength = mrNum.length;
                var nLength = numText.length;

                var point1 = isPoint(numText);
                var point2 = isPoint(mrNum);
                var pLength1 = nLength - point1.location - 1;
                var pLength2 = mLength - point2.location - 1;

                if(pLength1 > pLength2){
                    var length = pLength1;
                }
                else{
                    var length = pLength2;
                }

                mrNum = Number(mrNum);
                numText = Number(numText);
                mrNum += numText;
                if(length > 0){
                    mrNum = mrNum.toFixed(length);
                }


                flagMr = false;
            });

            var deleteBut = rendButton(deleteImg,195+sevenImg.width*3 + maddImg.width,398 - zeroImg.height*4,deleteImg.width,deleteImg.height);
            deleteBut.addEventListener("mousedown",function(){
                numText = numText.toString();
                var length = numText.length;
                var finalchar = numText.slice(length-1,length);
                if(finalchar == "."){
                    flagPoint = true;
                }
                numText = numText.slice(0,length-1);
                if(numText =="" || numText == "-"){
                    numText = "0";
                    text.text = numText;
                }

                else{
                    text.text = numText;
                }
                
                calculationContainer.updateCache();
                update = true;
            });

            

            calculationContainer.addChild(panel,text,acBut,pointBut,
                addtBut,subtBut,mulBut,perBut,equalBut,pmBut,divBut,sqrtBut,
                zeroBut,doubleZeroBut,oneBut,twoBut,threeBut,fourBut,fiveBut,sixBut,sevenBut,eightBut,ninetBut,
                mcBut,mrBut,msubtBut,maddBut,deleteBut);
            calculationContainer.cache(0,0,w,h);

            createjs.Touch.enable(stage);
            createjs.Ticker.addEventListener("tick", tick);
            stage.addChild(calculationContainer);
        }

        function tick(event) {
            // this set makes it so the stage only re-renders when an event handler indicates a change has happened.
            if (update) {
                update = false; // only update once
                stage.update(event);
            }
        }
  

        function rendPanel(img,x,y,width,height){
            //计算器面板
            var panel = new createjs.Shape();
            panel.graphics.beginBitmapFill(img).drawRect(0,0,width,height);
            panel.x = x;
            panel.y = y;
            //panel.cursor = "pointer";
            panel.addEventListener("mousedown",function(event){
                calculationContainer.offset = {x:event.stageX - calculationContainer.x,y:event.stageY - calculationContainer.y}
            });

            panel.addEventListener("pressmove",function(event){
                calculationContainer.x = event.stageX - calculationContainer.offset.x;
                calculationContainer.y = event.stageY - calculationContainer.offset.y;

                update = true;
            });
            
            return panel;
        }

        

        function rendText(text){
            //计算结果
            var numText = new createjs.Text(text, "bold 50px Arial", "black");
            
            var y = 130;
            
            numText.x = 420;
            numText.y = 130;
            numText.textAlign = "right";
            
            return numText;
        }

        function rendButton(img,x,y,width,height){
            //计算器按钮
            var button = new createjs.Shape();
            button.graphics.beginBitmapFill(img).drawRect(0,0,width,height);
            button.x = x;
            button.y = y;
            button.cursor = "pointer";

            return button;
        }

        function reRendtext_num(num){
            var child = stage.children[0];
            var cLength = child.numChildren;

            if(!flagRe){
                child.removeChildAt(cLength-1);

                flagRe = true;
            }

            if((Number(numText) == 0 && flagPoint) || (!flagEqual && flagPoint) ||(!flagMr)){
                numText = num;
                if(!flagEqual){
                    opnum2 = "";
                }
                flagEqual = true;
                flagMr = true;
            }
            else{
                text.font = "bold 50px Arial"
                numText = numText+num;
            }

            var i = numText.length-1;
            if(i >8){
                var point = isPoint(numText);
                if(point.bool){
                    text.font = "bold 48px Arial"
                    numText = numText.slice(0,10);
                }
                else{
                    text.font = "bold 50px Arial"
                    numText = numText.slice(0,9);
                }
            }

            text.text = numText;
            calculationContainer.updateCache();
            update = true;
        }

        function reRendtext_op1(operation){
            flagPoint = true;
            flagPercent =true;
            //if(flagEqual){
                if(op != operation && opNum1 == ""){
                    op = operation;
                    if(numText !=""){
                        opNum1 = numText;
                    }
                    numText = "";

                    //console.log(111)
                }

                //opNum2 = "";
                if(opNum1 != "" && opNum2 ==""){
                    opNum2 = numText;
                    switch(op){
                        case "+": if(opNum2 ==""){
                                      opNum2 = 0;
                                      numText = Number(opNum1) + 0;
                                  }
                                  else{
                                      numText = Number(opNum1) + Number(opNum2);
                                  }
                                  
                                  break;
                        case "-": if(opNum2 ==""){
                                      opNum2 = 0;
                                      numText = Number(opNum1) - 0;
                                  }
                                  else{
                                      numText = Number(opNum1) - Number(opNum2);
                                  }
                                  
                                  break; 
                        case "*": if(opNum2 ==""){
                                      opNum2 = 1;
                                      numText = Number(opNum1) * 1;
                                  }
                                  else{
                                      numText = Number(opNum1) * Number(opNum2);
                                  }
                                  break;
                        case "/": if(opNum2 ==""){
                                      opNum2 = 1
                                      numText = Number(opNum1) / 1;
                                  }
                                  else{
                                      numText = Number(opNum1) / Number(opNum2);
                                  }

                                  break;
                    }

                    opNum1 = opNum1.toString();
                    opNum2 = opNum2.toString();
                    var op1Length = opNum1.length;
                    var op2Length = opNum2.length;
                    
                    var point1 = isPoint(opNum1);
                    var point2 = isPoint(opNum2);
                    var pLength1 = op1Length - point1.location - 1;
                    var pLength2 = op2Length - point2.location - 1;

                    if(pLength1 >= pLength2 && op != "*"){
                        var length = pLength1;
                    }
                    else if(pLength1 <= pLength2 && op != "*"){
                        var length = pLength2;
                    }
                    else if(pLength1 >= pLength2 && op == "*"){
                        var length = pLength1+pLength2;
                    }
                    else if(pLength1 <= pLength2 && op == "*"){
                        var length = pLength1+pLength2;
                    }


                    numText = Number(numText);
                    if(!isE(numText)){
                        if(numText<=99999999 || numText == "Infinity"){
                            if(op != "/"){
                                numText = numText.toFixed(length);
                            }

                            numText = numText.toString();
                            if(numText.length>9 && (isPoint(numText).bool) ){
                                numText = numText.slice(0,10);
                                if(numText.slice(9,10)=="."){
                                    text.font="bold 50px Arial";
                                    numText = numText.slice(0,9);
                                }      
                                else{
                                    text.font="bold 48px Arial";
                                }                 
                            }
                            else{
                            
                                numText = numText.slice(0,9);
                                text.font = "bold 50px Arial";

                            }

                            //console.log(opNum1,op,opNum2);
                            if(op == "/" && opNum2 == 0){
                                numText = 0;
                                if(flagRe){
                                    var eText = reRendtext_e();
                                    calculationContainer.addChild(eText);
                                    flagRe = false;
                                }
                            }
                        }
                        else{
                            result = numText;

                            result = result.toString();
                            if(result.length>9 && (isPoint(result).bool) ){
                                result = result.slice(0,10);
                                if(result.slice(9,10)=="."){
                                    text.font="bold 50px Arial";
                                    result = result.slice(0,9);
                                }      
                                else{
                                    text.font="bold 48px Arial";
                                }                 
                            }
                            else{
                            
                                result = result.slice(0,9);
                                text.font = "bold 50px Arial";

                                
                            }
                            numText = lastResult;
                        }
                    }

                    else{
                        numText ="0";
                        opNum1 = "";
                        opNum2 = "";
                    }
                    
                    lastResult = numText;
                    if(result != ""){
                        text.font="bold 48px Arial";
                        text.text = result;
                        if(flagRe){
                            var eText = reRendtext_e();
                            calculationContainer.addChild(eText);
                            flagRe = false;
                        }
                    }
                    else{
                        text.text = numText;
                    }
                    calculationContainer.updateCache();
                    update = true;
                    opNum1 = numText;
                    lastResult = numText;
                    numText = "";
                    opNum2 = "";
                    //console.log(222)
                }
                else{
                    opNum1 = numText;
                    opNum2 = "";
                    numText = "";
                }

                if(op != operation && opNum1 !=""){
                    op = operation;
                    numText = "";
                }

            

            
            
        }

        function reRendtext_e(){
            var eText = new createjs.Text("E", "bold 25px Arial", "black");
            eText.name = "e";                     
            eText.x = 160;
            eText.y = 155;
            flagRe = false;
            flagEqual = true;
            return eText;
        }

        

        function isPoint(str){
            str = str.toString();
            var point={bool:false,location:0};

            for( var i = 0;i < str.length;i++){
                if(str[i] == "."){
                    point.bool = true;
                    point.location = i;
                }
            }
            if(point.location == 0){
                point.location = str.length-1;
            }
            return point;
        }

        function isE(str){
            str = str.toString();
            var bool=false;

            for( var i = 0;i < str.length;i++){
                if(str[i] == "e"){
                    bool = true;
                }
            }
            return bool;
        }


    </script>
</head>
<body onload="init();">
    <div>
        <canvas id="calculation" width="900" height="800" style="background: white"></canvas>
    </div>
</body>
</html>